import {Component, ViewChild} from '@angular/core';
import {Nav, Platform} from 'ionic-angular';
import {StatusBar} from '@ionic-native/status-bar';
import {SplashScreen} from '@ionic-native/splash-screen';

import {ACLService} from "./acl/acl.service";
import {AppConfig} from "./appConfig";
import {HttpClient} from "@angular/common/http";
import {AppUrl, STORAGE_KEY, TIME_OUT} from "./app.general";
import {TranslateService} from "@ngx-translate/core";
import 'rxjs/add/operator/timeout'
import {Storage} from "@ionic/storage";
import {MainPage} from "../pages/pages";

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage: any = MainPage;
  @ViewChild(Nav) nav: Nav;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private aclService: ACLService, private appConfig: AppConfig, private http: HttpClient, private translate: TranslateService, private storage: Storage) {
    platform.ready().then(() => {
      let token = localStorage.getItem(STORAGE_KEY.token);
      console.log('tsaetse' + token);
      this.aclService.clearAll();

      // if (platform.is('mobileweb')||platform.is('core')){
      // }
      // if (false) {
      // }
      if (platform.url().includes('food-home') ||
        platform.url().includes('go404') ||
        platform.url().includes('ali-pay-success') ||
        platform.url().includes('confirm-order')) {
      }
      else {
        this.appConfig.presentLoading('正在加载...', TIME_OUT);

        if (token && token !== 'undefined') {
          this.http.get(AppUrl.auth.refresh).timeout(TIME_OUT).subscribe(res => {
            let temp: any = res;
            if (temp.code === 200) {
              if (temp.data.token) {
                localStorage.setItem(STORAGE_KEY.token, temp.data.token);
                this.storage.set(STORAGE_KEY.userName, temp.data.userName);
                this.aclService.setAbilities(temp.data.abilities);
                this.aclService.setRoles([temp.data.role]);
                this.nav.setRoot('TabsPage', {}, {
                  animate: true,
                  direction: 'forward'
                }).then(this.appConfig.loader.dismiss());
              }
              else {
                this.appConfig.presentToast('登录状态已失效，请重新登录');
                this.appConfig.loader.dismiss();
              }
            }
            else {
              this.appConfig.presentToast(temp.msg);
              this.appConfig.loader.dismiss();
            }
          }, reject => {
            this.appConfig.presentToast('自动登录超时，请检查网络设置');
            this.appConfig.loader.dismiss();
            console.log(reject);
          });
        }
        else {
          this.appConfig.loader.dismiss();
        }
      }
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.show();
      splashScreen.hide();
    });
    this.init();
  }

  init() {
    this.translate.setDefaultLang('zh-cmn-Hans');     //默认语言-英
    const browserLang = this.translate.getBrowserLang(); //获取浏览器使用的语言
    if (browserLang) {
      if (browserLang === 'zh') {
        const browserCultureLang = this.translate.getBrowserCultureLang();

        if (browserCultureLang.match(/-CN|CHS|Hans/i)) {
          //使用简体中文
          this.translate.use('zh-cmn-Hans');
        } else if (browserCultureLang.match(/-TW|CHT|Hant/i)) {
          this.translate.use('zh-cmn-Hans');
        }
      } else {
        this.translate.use('zh-cmn-Hans'); // Set your language here
      }
    } else {
      this.translate.use('zh-cmn-Hans'); // Set your language here
    }
  }
}
